package demo7;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.IOException;
import java.net.Socket;

@Component("classpath:config.properties")
public class Client {
    private String url;
    private int port;
    private Socket socket;

    public String getUrl() {
        return url;
    }

    @Value("${server.url}")
    public void setUrl(String url) {
        this.url = url;
    }

    public int getPort() {
        return port;
    }

    @Value("${server.port}")
    public void setPort(int port) {
        this.port = port;
    }
    @PostConstruct
    public void init() {
        try {
            socket = new Socket(url, port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @PreDestroy
    public void destroy() {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
